package zhh.tangbao.auxiliary.scheduleTask.staticScheduleTask;

import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import zhh.tangbao.auxiliary.dao.DataStatisticsLogCommodityDao;
import zhh.tangbao.auxiliary.dao.DataStatisticsLogDao;
import zhh.tangbao.auxiliary.entity.DataStatisticsLog;
import zhh.tangbao.auxiliary.entity.DataStatisticsLogCommodity;
import zhh.tangbao.commodity.constant.OrderStatus;
import zhh.tangbao.commodity.entity.CommodityOrder;
import zhh.tangbao.commodity.entity.CommodityOrderContent;
import zhh.tangbao.commodity.serivce.CommodityOrderService;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 关于订单的定时任务，用来进行订单数据统计
 * </p>
 *
 * @author 粥灰灰
 * <p>2023/4/27</p>
 */

@Component
@EnableScheduling
public class OrderScheduleTask {

    @Autowired
    private CommodityOrderService service;

    @Autowired
    private DataStatisticsLogDao logDao;

    @Autowired
    private DataStatisticsLogCommodityDao logCommodityDao;

    /**
     * 统计前一天的商品销售数据
     */
    @Scheduled(cron = "0 0 0 1/1 * ?")
    public void commoditySellStatistics() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.add(Calendar.SECOND, -1);
        Date end = c.getTime();
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.HOUR_OF_DAY, 0);
        Date start = c.getTime();
        List<CommodityOrder> commodityOrders = service.allOrderDuring(start, end, OrderStatus.PICKED);
        if (CollUtil.isEmpty(commodityOrders)) {
            return;
        }
        List<CommodityOrderContent> contents = service.batchSelectContentByOrderIds(commodityOrders.stream().map(CommodityOrder::getId).collect(Collectors.toList()));
        if (CollUtil.isEmpty(contents)) {
            return;
        }
        DataStatisticsLog log = new DataStatisticsLog();
        log.setRecordTime(new Date());
        log.setRecordStartTime(start);
        log.setRecordEndTime(end);
        logDao.insert(log);
        Map<String, List<CommodityOrderContent>> collect = contents.stream().collect(Collectors.groupingBy(CommodityOrderContent::getCommodityEntityId));
        if (CollUtil.isEmpty(collect)) {
            return;
        }
        final Integer logId = log.getId();
        List<DataStatisticsLogCommodity> insertList = Lists.newArrayList();
        collect.forEach((eid, content) -> {
            DataStatisticsLogCommodity dlc = new DataStatisticsLogCommodity();
            dlc.setDataStatisticsLogId(logId);
            dlc.setCommodityEntityId(eid);
            dlc.setCompletedSellCount(content.stream().mapToInt(CommodityOrderContent::getCommodityEntityCount).sum());
            insertList.add(dlc);
        });
        logCommodityDao.insertBatchSomeColumn(insertList);
    }

}
